package org.youyun56.gpsserver.command;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.log4j.Logger;
import org.youyun56.gpsserver.CacheFactory;
import org.youyun56.gpsserver.Task;
import org.youyun56.gpsserver.common.YouyunMap;
import org.youyun56.gpsserver.entity.Good;

public class AddGoodExecutor extends Task {
    private final static Logger log = Logger.getLogger(AddGoodExecutor.class);

    @Override
    public Object execute() {
        try {
            Good good = new Good();
            good.setAddTime(new Timestamp(System.currentTimeMillis()));
            YouyunMap request = command.getParameters();
            good.setContact(getDecode(request.get("contact")));
            good.setFromCity(request.getInt("fromCity"));
            good.setTargetCity(request.getInt("targetCity"));
            good.setToCity(request.getInt("toCity"));
            good.setTxt(getDecode(request.get("txt")));
            good.setUserId(request.getLong("userId"));
            good.setAddTime(new Date(System.currentTimeMillis()));
            good.setCarType(getDecode(request.get("carType")));
            good.setCostTxt(request.get("costTxt"));
            good.setCost(request.getFloat("cost"));
            good.setCostUnin(getDecode(request.get("costUnin")));
            good.setHearvy(request.getFloat("hearvy"));
            good.setHearvyUnin(getDecode(request.get("hearvyUnin")));
            good.setHeavyType(getDecode(request.get("hearvyType")));
            good.setLian(request.getInt("lian"));
            good.setMi(request.getFloat("mi"));
            CacheFactory.instance.addGoodCache(good.getTargetCity(), good);
            return good;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }

    }

    public String getDecode(String name) throws UnsupportedEncodingException {

        return name == null ? null : URLDecoder.decode(name, "utf-8");
    }
}
